package com.codefish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.codefish.entity.InMoneyType;
import com.codefish.mapper.InMoneyTypeDao;
import com.codefish.service.InMoneyTypeService;
import com.codefish.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.List;

@Service("inMoneyTypeService")
public class InMoneyTypeServiceImpl extends ServiceImpl<InMoneyTypeDao, InMoneyType> implements InMoneyTypeService {

    @Autowired
    private SysUserTokenService userService;

    /**
     * 新增单个
     *
     * @param inMoneyType
     * @return
     */
    @Override
    public int saveOne(InMoneyType inMoneyType) {

        boolean saveFlag = this.save(inMoneyType);

        if (!saveFlag) {
            return -1;
        }

        return inMoneyType.getId();
    }

    /**
     * 修改单个
     *
     * @param inMoneyType
     * @return
     */
    @Override
    public boolean updateOne(InMoneyType inMoneyType) {

        boolean updateFlag = this.updateById(inMoneyType);

        if (updateFlag) {
            return true;
        }

        return false;
    }

    /**
     * 条件分页查询
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<InMoneyType> pagePageByParams(Long userId, Integer pageNo, Integer pageSize) {

        Page<InMoneyType> page = new Page<>(pageNo, pageSize);

        // familyId
        Integer familyId = null;

        familyId = userService.getFamilyId(userId);

        QueryWrapper<InMoneyType> qw = new QueryWrapper<>();
        qw.eq("state",1).ne("is_del",1);
        qw.eq("family_id",familyId);
        qw.orderByDesc("id");

        IPage<InMoneyType> retIPage = baseMapper.selectPage(page, qw);

        return retIPage;
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @Override
    public boolean deleteOne(Integer id) {

        InMoneyType inMoneyType = new InMoneyType();
        inMoneyType.setIsDel(1);
        inMoneyType.setId(id);

        boolean flag = this.updateById(inMoneyType);

        return flag;
    }

    /**
     * 查询所有
     * @param familyId
     * @return
     */
    @Override
    public List<InMoneyType> getAll(Integer familyId) {

        List<InMoneyType> inMoneyTypes = baseMapper.selectList(Wrappers.<InMoneyType>lambdaQuery()
                .eq(InMoneyType::getState, 1)
                .ne(InMoneyType::getIsDel, 1)
                .eq(InMoneyType::getFamilyId, familyId));
        if (CollectionUtils.isEmpty(inMoneyTypes)){
            return Collections.emptyList();
        }

        return inMoneyTypes;
    }

    /**
     * 根据id批量查询
     * @param ids
     * @return
     */
    @Override
    public List<InMoneyType> selectList(List<Integer> ids) {

        QueryWrapper<InMoneyType> qw = new QueryWrapper<>();
        qw.in("id",ids);

        return baseMapper.selectList(qw);
    }

}